package ArrayPackage2;

/**
 * @author Lzm
 * @version 1.0
 */
public class search_ {
  public static void main(String[] args) {
    int[] nums = new int[]{-1,0,3,5,9,12};
    int target = 2;
    int result = search(nums, target);
    System.out.println(result);
  }
  public static int search(int[] nums, int target) {
    // 剪枝
    if (nums.length == 0 || nums == null){
      return -1;
    }
    if (target < nums[0] || target > nums[nums.length - 1]){
      return -1;
    }
    // 定义双指针
    int left = 0;
    int right = nums.length - 1;
    while(left < right){
      int middle = ( right - left ) / 2 + left;
      if (nums[middle] == target){
        return middle;
      }
      else if(nums[middle] > target){
        right--;
        continue;
      }
      else if(nums[middle] < target){
        left++;
        continue;
      }
    }
    return -1;
  }
}
